    //
//  DatePickerViewController.m
//  StanfordEvents
//
//  Created by Gongmyung on 7/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

/*
 DateViewController.m
 */
#import "DatePickerViewController.h"

@implementation DatePickerViewController
@synthesize datePicker;
@synthesize dateTableView;
@synthesize date;
@synthesize delegate;

- (id) init {
    firstTime = YES;
    NSLog(@"firstTime set to YES");
    return [super init];
}

-(IBAction)dateChanged
{
    self.date = [datePicker date];
    [dateTableView reloadData];
    NSArray *cells = [dateTableView visibleCells];
    for (UITableViewCell *cell in cells) {
        if( [dateTableView indexPathForCell:cell].row == selected ) {
            [(DatePickerTableCell*)cell turnMeOn];
        }
        else {
            [(DatePickerTableCell*)cell turnMeOff];
        }
    }
}

-(IBAction)cancel
{
    [[self.delegate navController] popViewControllerAnimated:YES];
}

-(IBAction)save
{
    [self.delegate setStartDate:startDateSince1970 endDate:endDateSince1970];
    [[self.delegate navController] popViewControllerAnimated:YES];
}


- (void)loadView
{
    UIView *theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = theView;
    [theView release];
    
    UITableView *theTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 33.0, 320.0, 480.0) style:UITableViewStyleGrouped];
    theTableView.delegate = self;
    theTableView.dataSource = self;
    [self.view addSubview:theTableView];
    self.dateTableView = theTableView;
    [theTableView release];
    
    UIDatePicker *theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 200.0, 320.0, 216.0)];
    theDatePicker.datePickerMode = UIDatePickerModeDateAndTime;
    self.datePicker = theDatePicker;
    [theDatePicker release];
    [datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];
    
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
                                     initWithTitle:NSLocalizedString(@"Cancel", @"Cancel - for button to cancel changes")
                                     style:UIBarButtonItemStylePlain
                                     target:self
                                     action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = cancelButton;
    [cancelButton release];
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                   initWithTitle:NSLocalizedString(@"Save", @"Save - for button to save changes")
                                   style:UIBarButtonItemStylePlain
                                   target:self
                                   action:@selector(save)];
    self.navigationItem.rightBarButtonItem = saveButton;
    [saveButton release];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    if (self.date != nil)
        [self.datePicker setDate:date animated:YES];
    else 
        [self.datePicker setDate:[NSDate date] animated:YES];
    
    if( firstTime == YES ) {
        NSLog(@"GOT HEE");
        DatePickerTableCell *startDateCell = (DatePickerTableCell*)[dateTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        DatePickerTableCell *endDateCell = (DatePickerTableCell*)[dateTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        [startDateCell turnMeOn];
        [endDateCell turnMeOff];
        firstTime == NO;
    }
    
    [super viewWillAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc 
{
    [datePicker release];
    [dateTableView release];
    [date release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *DateCellIdentifier = @"datePickerCell";
    
    DatePickerTableCell *cell = (DatePickerTableCell*)[tableView dequeueReusableCellWithIdentifier:DateCellIdentifier];
    if (cell == nil) 
    {
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"DatePickerTableCell" owner:nil options:nil];
        
        for(id currentObject in nibObjects ) {
            if([currentObject isKindOfClass:[DatePickerTableCell class]])
                cell = (DatePickerTableCell*)currentObject;
        }
        
        cell.dateLabel.textColor = [UIColor colorWithRed:0.243 green:0.306 blue:0.435 alpha:1.0];
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    
    //if we are on start:
    if( indexPath.row == 0 ) {
        cell.leftSideLabel.text = @"Starts:";
        if ( selected == 0 ) {
            cell.dateLabel.text = [dateFormatter stringFromDate:date];
            startDateSince1970 = [date timeIntervalSince1970];
            [dateFormatter release];
        }
    }
    //if we are on end
    else if( indexPath.row == 1 ) {
        cell.leftSideLabel.text = @"Ends:";
        if ( selected == 1) {
            cell.dateLabel.text = [dateFormatter stringFromDate:date];
            endDateSince1970 = [date timeIntervalSince1970];
            [dateFormatter release];
        }
    }
    
    self.title = @"Select Dates";
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selected = indexPath.row;
    DatePickerTableCell *cell = (DatePickerTableCell*)[tableView cellForRowAtIndexPath:indexPath];
    [cell turnMeOn];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    selected = indexPath.row;
    DatePickerTableCell *cell = (DatePickerTableCell*)[tableView cellForRowAtIndexPath:indexPath];
    [cell turnMeOff];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;   
}
@end